﻿#include "pch.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
#include <unordered_map>
using namespace std;

TEST(vector, remove_if1) {
	std::vector<int> numbers = {1, 5, 2, 7, 8, 3, 9};
	int threshold = 6; // 你想要删除大于此阈值的元素

	// 使用 STL 的 remove_if 算法来移动需要删除的元素到向量末尾
	numbers.erase(std::remove_if(numbers.begin(), numbers.end(), [threshold](int value) {
		return value > threshold;
	}), numbers.end());

	// 打印结果
	for (int number : numbers) {
		std::cout << number << " ";
	}
	std::cout << std::endl;
}


TEST(vector, insert1) {
	// 创建第一个 vector
	std::vector<int> vec1 = {1, 2, 3};

	// 创建第二个 vector
	const std::vector<int> vec2 = {4, 5, 6};

	// 使用 insert 函数的范围版本将第二个 vector 的元素插入到第一个 vector 的尾部
	vec1.insert(vec1.end(), vec2.begin(), vec2.end());

	// 输出结果
	std::cout << "Combined Vector: ";
	for (int elem : vec1) {
		std::cout << elem << " ";
	}
	std::cout << std::endl;
}

struct TT {
	int id;
	std::string name;
};

TEST(vector, replace) {
	vector<TT> tts = {{298, "04aUxR9N"}, {841, "4jV852F"}, {690, "1q2j493"}};
	for (auto element : tts) {
		cout << element.id <<
			" " << element.name << endl;
	}
	TT be = {150, "2Y7"};
	auto it = ranges::find_if(tts, [](const TT& tt) { return tt.id == 298; });
	if (it != tts.end()) {
		*it = be;
	}
	for (auto element : tts) {
		cout << element.id << " " << element.name << endl;
	}
}

TEST(vector, insert) {
	vector<TT> tts = {{298, "04aUxR9N"}, {841, "4jV852F"}, {690, "1q2j493"}};
	for (auto element : tts) {
		cout << element.id <<
			" " << element.name << endl;
	}
	TT be = {150, "2Y7"};
	tts.insert(tts.begin() + 0, be);
	for (auto element : tts) {
		cout << element.id << " " << element.name << endl;
	}

	TT be_last = {150, "asd"};
	tts.insert(tts.begin() + tts.size(), be_last);
	for (auto element : tts) {
		cout << element.id << " " << element.name << endl;
	}
}

TEST(unorder_map, m1) {
	unordered_map<int, int> map = {
		pair<int, int>(1, 2),
		pair<int, int>(3, 4)
	};
	for (pair<int, int> kv : map) {
		cout << kv.first << kv.second << endl;
	}
	for (auto kv : map) {
		cout << kv.first << kv.second << endl;
	}
	for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {
		cout << it->first << it->second << endl;
	}
}

TEST(vector, fill1) {
	std::vector<int> vs(10);
	std::ranges::fill(vs, -1);
	for (auto v : vs) {
		cout << v << endl;
	}
}


TEST(vector, reduce1) {
	vector<TT> tts = {{298, "04aUxR9N"}, {841, "4jV852F"}, {690, "1q2j493"}};
	const auto res = std::reduce(tts.begin(), tts.end(), 1, [](auto acc, auto& elem) { return acc + elem.id; });
	cout << res << endl;
}

void fn_in_fn() {
	const auto in_fn = []() {
		cout << "in_fn" << endl;
	};
	for (int i = 0; i < 10; ++i) {
		in_fn();
	}
}

TEST(fns, fn_in_fn) {
	fn_in_fn();
}

TEST(swap, s1) {
	vector<TT> vec = {{298, "04aUxR9N"}, {841, "4jV852F"}, {690, "1q2j493"}};


	// 使用迭代器交换元素
	std::vector<TT>::iterator it1 = vec.begin() + 0; // 指向元素 2
	std::vector<TT>::iterator it2 = vec.begin() + 2; // 指向元素 4

	// 交换元素
	std::swap(*it1, *it2);

	// 输出向量
	for (auto num : vec) {
		std::cout << num.id << " " << num.name << endl;
	}
}

void shu(std::vector<int>& v) {
	static std::random_device rd;
	static std::mt19937 g(rd());

	ranges::shuffle(v, g);
}

TEST(shuffle, s2) {
	std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};


	for (int i = 0; i < 3; ++i) {
		shu(v);

		ranges::copy(v, std::ostream_iterator<int>(std::cout, " "));
		std::cout << '\n';
	}
}

bool is_even(int n) {
	return n % 2 == 0;
}

TEST(vectorPation, vp1) {
	vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

	// 将 v 按奇偶分成两个 vector
	vector<int> even_v, odd_v;
	auto it_pati = std::partition(v.begin(), v.end(), is_even);

	for (auto it = v.begin(); it != it_pati; it++) {
		cout << *it << " ";
	}
	cout << endl;
	for (auto it = it_pati; it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}


TEST(any_of, a1) {
	vector<TT> vec = {{298, "04aUxR9N"}, {841, "4jV852F"}, {690, "1q2j493"}};
	auto b = std::any_of(vec.begin(), vec.end(), [](TT item) { return item.id == 690; });
	auto b1 = std::any_of(vec.begin(), vec.end(), [](TT item) { return item.id == 691230; });
	cout << b << endl;
	cout << b1 << endl;
}

struct TTS {
private :

public:
	std::vector<TT> popus;

	inline TT getTT(int i) {
		return popus.at(i);
	}

	inline void setTT(std::vector<TT> input) {
		popus = input;
	}

	std::vector<TT> getTTS() {
		return popus;
	}
};

TEST(chuanyinyong, c1) {
	TTS ttNew;
	vector<TT> vec = {{298, "04aUxR9N"}, {841, "4jV852F"}, {690, "1q2j493"}};
	ttNew.setTT(vec);
	auto& c1 = ttNew.popus.at(1);
	auto& c2 = ttNew.popus[2];
	c1.id = 0;
	c2.id = 975;
	for (auto value : ttNew.getTTS()) {
		std::cout << value.id << " " << value.name << endl;
	}
	cout << "------------------------------------------------------------------------------------------------------------------------------------" << endl;

	for (auto value : vec) {
		std::cout << value.id << " " << value.name << endl;
	}
}


struct TTSIT {
private :
	std::vector<TT> popus;

public:
	inline vector<TT>::iterator getTT(int i) {
		return popus.begin() + i;
	}

	inline void setTT(std::vector<TT> input) {
		popus = input;
	}

	std::vector<TT> getTTS() {
		return popus;
	}
};

TEST(chuanyinyong, it1) {
	TTSIT ttNew;
	vector<TT> vec = {{298, "04aUxR9N"}, {841, "4jV852F"}, {690, "1q2j493"}};
	ttNew.setTT(vec);
	auto c1 = ttNew.getTT(1);
	auto c2 = ttNew.getTT(2);
	c1->id = 0;
	c2->id = 975;
	for (auto value : ttNew.getTTS()) {
		std::cout << value.id << " " << value.name << endl;
	}
	cout << "------------------------------------------------------------------------------------------------------------------------------------" << endl;

	for (auto value : vec) {
		std::cout << value.id << " " << value.name << endl;
	}
}

class PrivateC {
private:
	std::vector<std::pair<double, double>> ivs;

public:
	std::string name;
	std::vector<int> pubsints;

	PrivateC(std::vector<int> ins) : pubsints(ins) {
		name = "123";
	}
};

class PubC {
public:
	std::vector<int> ivs;
};

void PushPri(std::vector<PrivateC>& pcs) {
	PrivateC pc = PrivateC({1, 3});
	pcs.push_back(pc);
}

void PushPub(std::vector<PubC>& pcs) {
	PubC pc = PubC();
	pcs.push_back(pc);
}


/**
  * @brief
   * @author: lianbo
*/
TEST(PP, p1) {
	std::vector<PrivateC> pcs;
	PushPri(pcs);
	PushPri(pcs);

	std::vector<PubC> PubCs;
	PushPub(PubCs);
	cout << "done" << endl;
}

TEST(PVT, p1) {
	std::vector<PrivateC> pcs;
	PrivateC pc1({1, 2});
	pcs.push_back(pc1);
	PrivateC pc2({3, 4});
	pcs.push_back(pc2);

	for (auto value : pcs) {
		std::cout << value.name << " " << endl;
	}
}
